package logic

import (
	"context"
	"net/http"
	"path/filepath"
	"video/pkg"

	"video/api/internal/svc"
	"video/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoUploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewVideoUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoUploadLogic {
	return &VideoUploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *VideoUploadLogic) VideoUpload(req *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	file, m, err := req.FormFile("file")
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	if m.Size > 1024*1024*200 {
		return &types.Response{
			Message: "视频文件过大，最大支持200MB",
			Code:    500,
			Data:    nil,
		}, nil
	}

	ext := filepath.Ext(m.Filename)
	if ext != ".MP4" && ext != ".AVI" {
		return &types.Response{
			Message: "格式错误提示“仅支持MP4/AVI格式，请重新选择",
			Code:    500,
			Data:    nil,
		}, nil
	}

	upload, err := pkg.Upload(file, m)
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Message: "文件上传成功",
		Code:    200,
		Data:    upload,
	}, nil
}
